#include "precompile.h"

#include "PresentationRuntime.h"

#include "Presentation/context/PresentationContext.h"
#include "Presentation/IPresentationWorkflow.h"
#include "Presentation/IPresentation.h"

PresentationRuntime::PresentationRuntime()
{
}

PresentationRuntime::~PresentationRuntime()
{
}

void PresentationRuntime::RegisterPresentation(vclsid target,vclsid presentation)
{
	PresentationMap::iterator itFind = m_presentationMap.find(target);
	if (itFind != m_presentationMap.end())
		itFind->second = presentation;
	else
		m_presentationMap.insert(std::make_pair(target,presentation));
}

void PresentationRuntime::UnRegisterPresentation(vclsid target)
{
	PresentationMap::iterator itFind = m_presentationMap.find(target);
	if (itFind != m_presentationMap.end())
		m_presentationMap.erase(itFind);
}

void PresentationRuntime::DisablePresentation(vclsid target)
{
	m_disabledpresentationTargetSet.insert(target);
}

void PresentationRuntime::RegisterWorkflow(const String& kind,vclsid presentationWorkflow)
{
	PresentationWorkflowMap::iterator itFind = m_presetationWorkflowMap.find(kind);
	if (itFind != m_presetationWorkflowMap.end())
		itFind->second = presentationWorkflow;
	else
		m_presetationWorkflowMap.insert(std::make_pair(kind,presentationWorkflow));
}

void PresentationRuntime::UnRegisterWorkflow(const String& kind)
{
	PresentationWorkflowMap::iterator itFind = m_presetationWorkflowMap.find(kind);
	if (itFind != m_presetationWorkflowMap.end())
		m_presetationWorkflowMap.erase(itFind);
}

void PresentationRuntime::RunPresentation(Graphics* graphics,PresentationContext& context)
{
	if (IsPresentationEnabled(context.GetTargetClsid()))
	{
		sys::ObjectPtr objPresentation = CreatePresentation(context.GetTargetClsid());
		if (objPresentation.IsNotNull())
		{
			sys::InterfacePtr<IPresentation> pIFPresentation(objPresentation);
			pIFPresentation->Present(graphics,context);
		}
	}
}

void PresentationRuntime::RunWorkflow(IPresentationWorkflow* workflow,PresentationContext& context)
{
	m_disabledpresentationTargetSet.clear();

	if (workflow)
	{
		if (WorkflowRunning())
			WorkflowRunning()(workflow->GetKind(),workflow);
		workflow->Present(context);
		if (WorkflowRunned())
			WorkflowRunned()(workflow->GetKind(),workflow);
	}
}

sys::ObjectPtr PresentationRuntime::CreateWorkflow(const String& kind)
{
	sys::ObjectPtr objPresentationWorkflow;
	PresentationWorkflowMap::iterator itFind = m_presetationWorkflowMap.find(kind);
	if (itFind != m_presetationWorkflowMap.end())
	{
		objPresentationWorkflow.CreateObject(itFind->second);
		ASSERT(objPresentationWorkflow.IsNotNull());
		sys::InterfacePtr<IPresentationWorkflow> pIFPresentationWorkflow(objPresentationWorkflow);
		pIFPresentationWorkflow->SetKind(kind);
	}

	return objPresentationWorkflow;
}

//////////////////////////////////////////////////////////////////////////
//private method
bool PresentationRuntime::IsPresentationEnabled(vclsid target) const
{
	DisabledPresentationTargetSet::const_iterator itFind = m_disabledpresentationTargetSet.find(target);
	return ( itFind==m_disabledpresentationTargetSet.end() );
}

sys::ObjectPtr PresentationRuntime::CreatePresentation(vclsid target)
{
	sys::ObjectPtr objPresentation;
	PresentationMap::iterator itFind = m_presentationMap.find(target);
	if (itFind != m_presentationMap.end())
	{
		objPresentation.CreateObject(itFind->second);
	}
	return objPresentation;
}
